﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BLL;
using Model;
using Util;

namespace UI
{
    public partial class OperateOutKnifeUseForm : Form, InterfaceSelect
    {
        OperateInOutBLL bll = new OperateInOutBLL();
        PlaceAreaBLL sbll = new PlaceAreaBLL();
        PlaceBLL pbll = new PlaceBLL();
        RuningDoListBLL dbll = new RuningDoListBLL();
        MaterielBLL mbll = new MaterielBLL();
        MaterielTypeBLL tbll = new MaterielTypeBLL();
        StocksBLL stockBll = new StocksBLL();
        MachineBLL mabll = new MachineBLL();
        MachineBLL mBll = new MachineBLL();
        DataTable dtBak;
        DataTable dt;
        InOutType inOutType = InOutType.KNIFE_OUT_USE;
        public T_JB_Materiel materielNow;
        int max1, max2, max3, max4 = 0;
        float blank = 0;

        public OperateOutKnifeUseForm()
        {
            InitializeComponent();
        }

        private void OperateInForm_Load(object sender, EventArgs e)
        {
            initData();

            #region 初始化 物料类别
            DataTable dt = tbll.GetList(null, null, null, 1);
            DataView dataView = dt.DefaultView;
            dataView.Sort = "C_ID asc";
            cmbType.DataSource = dataView.ToTable();
            cmbType.DisplayMember = "C_NAME";
            cmbType.ValueMember = "C_ID";
            cmbType.SelectedIndex = -1;
            #endregion

            #region 初始化 货区
            DataTable dtt = sbll.GetList(null);
            DataView dataViewt = dtt.DefaultView;
            dataViewt.Sort = "C_ID asc";
            cmbArea.DataSource = dataViewt.ToTable();
            cmbArea.DisplayMember = "C_NAME";
            cmbArea.ValueMember = "C_ID";
            cmbArea.SelectedIndex = -1;
            #endregion

            #region 初始化 机床
            InitMachine();
            #endregion
        }

        private void InitMachine()
        {
            DataTable dtM = mBll.GetList();
            DataView dataViewtM = dtM.DefaultView;
            dataViewtM.Sort = "C_ID asc";
            cmbMachine.DataSource = dataViewtM.ToTable();
            cmbMachine.DisplayMember = "C_ID";
            cmbMachine.ValueMember = "C_ID";
            cmbMachine.SelectedIndex = -1;
        }


        //确认
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (checkInput())
            {
                setMain(false);
                addRow(dtBak);
                initSub();
            }
        }

        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //手工出库
        private void btnShouGong_Click(object sender, EventArgs e)
        {
            if (dt.Rows.Count <= 0)
            {
                MessageBox.Show("没有要入库的记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                try
                {
                    if (bll.HasDoList())
                    {
                        MessageBox.Show("存在未完成的联机任务，不能进行出入库操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }

                    List<string> listMachine = new List<string>();

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (!listMachine.Contains(dt.Rows[i][10].ToString()))
                        {
                            listMachine.Add(dt.Rows[i][10].ToString());
                        }
                    }

                    listMachine.ForEach(item => mabll.Save(item));

                    InitMachine();

                    string result = bll.handOut(dt, txtInMeno.Text, inOutType);

                    if (!string.IsNullOrEmpty(result))
                    {
                        MessageBox.Show("出库成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Log.saveLog("刀具使用出库成功！单号：" + result);
                        setMain(true);
                        initMain();
                        initSub();
                        dt.Rows.Clear();
                    }
                    else
                    {
                        MessageBox.Show("出库失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("与数据库连接失败，请查看网络连接是否正常。如不能解决请与网络管理员联系！", "严重错误：", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(cmbMachine.Text.Trim()))
            {
                MessageBox.Show("请先选择机床！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            dtBak = dt.Clone();

            SelectKnifeUseOutForm select = new SelectKnifeUseOutForm(null, dt, dtBak);
            select.ShowDialog();

            if (dtBak.Rows.Count > 0)
            {
                ModelToUI(mbll.getMaterielById(dtBak.Rows[0][0].ToString()));

                txtInPlace.Text = dtBak.Rows[0][4].ToString();

                max1 = Convert.ToInt32(dtBak.Rows[0][7].ToString()) >= 1 ? 1 : 0;
                max2 = Convert.ToInt32(dtBak.Rows[0][8].ToString()) >= 1 ? 1 : 0;
                max3 = Convert.ToInt32(dtBak.Rows[0][9].ToString()) >= 1 ? 1 : 0;
                max4 = Convert.ToInt32(dtBak.Rows[0][10].ToString()) >= 1 ? 1 : 0;

                int count1 = 0, count2 = 0, count3 = 0, count4 = 0;
                getMachineKnife(cmbMachine.Text.Trim(), dtBak.Rows[0][0].ToString(), out count1, out count2, out count3, out count4);

                cbCount1.Checked = max1 == 1 && count1 == 0;
                cbCount2.Checked = max2 == 1 && count2 == 0;
                cbCount3.Checked = max3 == 1 && count3 == 0;
                cbCount4.Checked = cbBoard.Checked && max4 == 1 && count4 == 0;
            }
        }

        //private void Select(float blank, string item)
        //{
        //    dtBak = dt.Clone();

        //    SelectKnifeUseOutForm select = new SelectKnifeUseOutForm(item, dt, dtBak);
        //    select.ShowDialog();

        //    if (dtBak.Rows.Count > 0)
        //    {
        //        ModelToUI(mbll.getMaterielById(dtBak.Rows[0][0].ToString()));

        //        txtInPlace.Text = dtBak.Rows[0][4].ToString();

        //        max1 = Convert.ToInt32(dtBak.Rows[0][7].ToString());
        //        max2 = Convert.ToInt32(dtBak.Rows[0][8].ToString());
        //        max3 = Convert.ToInt32(dtBak.Rows[0][9].ToString());
        //        max4 = Convert.ToInt32(dtBak.Rows[0][10].ToString());

        //        cbCount1.Text = "1";
        //        cbCount2.Text = blank > 2 ? "0" : "1";
        //        cbCount3.Text = blank > 2 ? "1" : "0";
        //        cbCount4.Text = cbBoard.Checked ? "1" : "0";
        //    }
        //}

        private bool CheckBarcode(string ids, ref string mes, ref List<T_JB_Materiel> listKnife, ref float blank)
        {
            if (ids.Length < 1)
            {
                mes = "码为空！";
                return false;
            }
            else
            {
                try
                {
                    string[] arr = ids.Split(new char[] { ',' });

                    if (arr.Length<2)
                    {
                        mes = "码无法解析！";
                        return false;
                    }

                    for (int i = 0; i < arr.Length - 1; i++)
                    {
                        T_JB_Materiel mo = mbll.getMaterielById(arr[i]);
                        if (mo == null)
                        {
                            mes = "码无法解析！";
                            return false;
                        }

                        listKnife.Add(mo);
                    }

                    blank = Convert.ToSingle(arr[arr.Length - 1]);
                }
                catch
                {
                    mes = "码无法解析！";
                    return false;
                }
            }

            return true;
        }

        //联机出库
        private void button2_Click(object sender, EventArgs e)
        {
            if (dt.Rows.Count <= 0)
            {
                MessageBox.Show("没有要出库的记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                try
                {
                    if (bll.HasDoList())
                    {
                        MessageBox.Show("存在未完成的联机任务，不能进行出入库操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }

                    List<string> listMachine = new List<string>();

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (!listMachine.Contains(dt.Rows[i][10].ToString()))
                        {
                            listMachine.Add(dt.Rows[i][10].ToString());
                        }
                    }

                    listMachine.ForEach(item => mabll.Save(item));

                    InitMachine();

                    string res = dbll.SaveDolist(dt, txtInMeno.Text, inOutType);

                    if (!string.IsNullOrEmpty(res))
                    {
                        MessageBox.Show("保存联机任务成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Log.saveLog("保存刀具使用出库联机任务成功！单号：" + res);
                        setMain(true);
                        initMain();
                        initSub();
                        dt.Rows.Clear();
                    }
                    else
                    {
                        MessageBox.Show("保存联机任务失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("与数据库连接失败，请查看网络连接是否正常。如不能解决请与网络管理员联系！", "严重错误：", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void addRow(DataTable dtBak)
        {
            DataRow dr = dt.NewRow();
            dr[0] = dtBak.Rows[0][0];
            dr[1] = dtBak.Rows[0][1];
            dr[2] = dtBak.Rows[0][2];
            dr[3] = cbCount1.Checked ? 1 : 0;
            dr[4] = cbCount2.Checked ? 1 : 0;
            dr[5] = cbCount3.Checked ? 1 : 0;
            dr[6] = cbCount4.Checked ? 1 : 0;
            dr[7] = dtBak.Rows[0][4];
            dr[8] = dtpIndate.Value.ToString("yyyy-MM-dd");
            dr[9] = dtBak.Rows[0][6];
            dr[10] = cmbMachine.Text.Trim();
            dr[11] = dtBak.Rows[0][11];

            bool flag = false;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string m = Convert.ToString(dt.Rows[i][0]);
                string p = Convert.ToString(dt.Rows[i][7]);
                string c = Convert.ToString(dt.Rows[i][10]);
                if (txtId.Text.Equals(m) && txtInPlace.Text.Trim().Equals(p) && cmbMachine.Text.Trim().Equals(c))
                {
                    flag = true;
                    dt.Rows[i][3] = Convert.ToInt32(dt.Rows[i][3]) + (cbCount1.Checked ? 1 : 0);
                    dt.Rows[i][4] = Convert.ToInt32(dt.Rows[i][4]) + (cbCount2.Checked ? 1 : 0);
                    dt.Rows[i][5] = Convert.ToInt32(dt.Rows[i][5]) + (cbCount3.Checked ? 1 : 0);
                    dt.Rows[i][6] = Convert.ToInt32(dt.Rows[i][6]) + (cbCount4.Checked ? 1 : 0);
                    break;
                }
            }

            if (flag == false)
            {
                dt.Rows.InsertAt(dr, 0);
            }
        }

        private void addRowMult(DataTable dtBak)
        {
            for (int j = 0; j < dtBak.Rows.Count; j++)
            {
                DataRow dr = dt.NewRow();
                dr[0] = dtBak.Rows[j][0];
                dr[1] = dtBak.Rows[j][1];
                dr[2] = dtBak.Rows[j][2];
                dr[3] = dtBak.Rows[j][7];
                dr[4] = dtBak.Rows[j][8];
                dr[5] = dtBak.Rows[j][9];
                dr[6] = dtBak.Rows[j][10];
                dr[7] = dtBak.Rows[j][4];
                dr[8] = dtpIndate.Value.ToString("yyyy-MM-dd");
                dr[9] = dtBak.Rows[j][6];
                dr[10] = cmbMachine.Text.Trim();
                dr[11] = dtBak.Rows[j][11];

                bool flag = false;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string m = Convert.ToString(dt.Rows[i][0]);
                    string p = Convert.ToString(dt.Rows[i][7]);
                    string c = Convert.ToString(dt.Rows[i][10]);
                    if (dtBak.Rows[j][0].ToString().Equals(m) && dtBak.Rows[j][4].ToString().Trim().Equals(p) && cmbMachine.Text.Trim().Equals(c))
                    {
                        flag = true;
                        dt.Rows[i][3] = Convert.ToInt32(dt.Rows[i][3]) + Convert.ToInt32(dtBak.Rows[j][7]);
                        dt.Rows[i][4] = Convert.ToInt32(dt.Rows[i][4]) + Convert.ToInt32(dtBak.Rows[j][8]);
                        dt.Rows[i][5] = Convert.ToInt32(dt.Rows[i][5]) + Convert.ToInt32(dtBak.Rows[j][9]);
                        dt.Rows[i][6] = Convert.ToInt32(dt.Rows[i][6]) + Convert.ToInt32(dtBak.Rows[j][10]);
                        break;
                    }
                }

                if (flag == false)
                {
                    dt.Rows.InsertAt(dr, 0);
                }
            }
        }

        private void initData()
        {
            dt = new DataTable();

            for (int i = 0; i < 12; i++)
            {
                DataColumn column = new DataColumn();
                column.DataType = System.Type.GetType("System.String");
                column.ColumnName = i.ToString();

                dt.Columns.Add(column);
            }



            this.dgv_Data.DataSource = dt;
            getName();
        }

        private void getName()
        {
            dgv_Data.Columns[0].HeaderText = "物料编码";
            dgv_Data.Columns[1].HeaderText = "物料名称";
            dgv_Data.Columns[2].HeaderText = "规格型号";
            dgv_Data.Columns[3].HeaderText = "上模数量";
            dgv_Data.Columns[4].HeaderText = "0.25数量";
            dgv_Data.Columns[5].HeaderText = "0.5数量";
            dgv_Data.Columns[6].HeaderText = "退料板数量";
            dgv_Data.Columns[7].HeaderText = "货位";
            dgv_Data.Columns[8].HeaderText = "入库日期";
            dgv_Data.Columns[9].HeaderText = "操作员";
            dgv_Data.Columns[9].Visible = false;
            dgv_Data.Columns[10].HeaderText = "机床";
            dgv_Data.Columns[11].HeaderText = "首次入库日期";
            dgv_Data.Columns[11].Visible = false;
        }

        private bool checkInput()
        {
            bool flag = true;
            if (Convert.ToDateTime(dtpIndate.Value.ToShortDateString()) > Convert.ToDateTime(DateTime.Now.ToShortDateString()))
            {
                MessageBox.Show("出库日期不能大于当前日期!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                flag = false;
            }

            if (txtMaterielName.Text == null || string.Empty.Equals(txtMaterielName.Text))
            {
                flag = false;
                this.lblMaterielName.Visible = true;
            }
            else
            {
                this.lblMaterielName.Visible = false;
            }

            if (string.IsNullOrEmpty(cmbMachine.Text.Trim()))
            {
                flag = false;
                MessageBox.Show("机床不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (cbCount1.Checked && max1 == 0)
            {
                flag = false;
                MessageBox.Show("上模数量大于可用数量！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (cbCount2.Checked && max2 == 0)
            {
                flag = false;
                MessageBox.Show("0.25数量大于可用数量！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (cbCount3.Checked && max3 == 0)
            {
                flag = false;
                MessageBox.Show("0.5数量大于可用数量！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (cbCount4.Checked && max4 == 0)
            {
                flag = false;
                MessageBox.Show("退料板数量大于可用数量！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (!(cbCount1.Checked||cbCount2.Checked||cbCount3.Checked||cbCount4.Checked))
            {
                flag = false;
                MessageBox.Show("请选择刀具！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            DataTable st = bll.getKDList(Global.minValue, Global.minValue, null, cmbMachine.Text.Trim(), InOutType.KNIFE_OUT_USE, null, null);
            int count1 = 0, count2 = 0, count3 = 0, count4 = 0;

            getMachineKnife(cmbMachine.Text.Trim(), txtId.Text.Trim(), out count1, out count2, out count3, out count4);

            if (cbCount1.Checked && count1 == 1)
            {
                flag = false;
                MessageBox.Show("该机床已有上模！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (cbCount2.Checked && count2 == 1)
            {
                flag = false;
                MessageBox.Show("该机床已有0.25！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (cbCount3.Checked && count3 == 1)
            {
                flag = false;
                MessageBox.Show("该机床已有0.5！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (cbCount4.Checked && count4 == 1)
            {
                flag = false;
                MessageBox.Show("该机床已有退料板！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            return flag;
        }

        private void setMain(bool flag)
        {
            dtpIndate.Enabled = flag;
            txtInMeno.Enabled = flag;
        }
        private void initMain()
        {
            txtInMeno.Text = string.Empty;
        }

        private void initSub()
        {
            txtMaterielName.Text = string.Empty;
            txtId.Text = string.Empty;
            txtStand.Text = string.Empty;
            txtInPlace.Text = string.Empty;
            txtMT.Text = string.Empty;
            txtLT.Text = string.Empty;
            txtWT.Text = string.Empty;
            txtMeno.Text = string.Empty;
            txtId.Text = string.Empty;
            this.txtDim1.Text = string.Empty;
            this.txtDim2.Text = string.Empty;
            this.txtDim3.Text = string.Empty;
            this.txtAngle.Text = string.Empty;
            this.txtRL.Text = string.Empty;
            cbBoard.Checked = false;
            cbCount1.Checked = false;
            cbCount2.Checked = false;
            cbCount3.Checked = false;
            cbCount4.Checked = false;
        }

        private void dgv_Data_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                string materiel = dt.Rows[e.RowIndex][0].ToString();
                int i = 0;
                for (; i < dt.Rows.Count; i++)
                {
                    if (materiel.Equals(dt.Rows[i][0]))
                    {
                        break;
                    }
                }
                if (i < dt.Rows.Count)
                {
                    dt.Rows.Remove(dt.Rows[i]);
                }
                if (dt.Rows.Count <= 0)
                {
                    setMain(true);
                }
            }
        }

        #region InterfaceSelect 成员

        public void setMateriel(string name, string id)
        {
            throw new NotImplementedException();
        }
        public void setMateriel(string name, string id, string standard)
        {
            this.txtMaterielName.Text = name;
            this.txtId.Text = id;
            this.txtStand.Text = standard;
        }
        public void setMateriel(string name, string id, int thick, int single, string standard, int length, int width)
        {
            throw new NotImplementedException();
        }

        public void setPlace(string name, string id, int length, int width)
        {
            this.txtInPlace.Text = id;
        }
        public void setMaterielAndPlace(string mname, string mid, string standard, string pid, string tray, int count, string typeName)
        {
            try
            {
                materielNow = mbll.getMaterielById(mid);
            }
            catch (Exception)
            {
                MessageBox.Show("与数据库连接失败，请查看网络连接是否正常。如不能解决请与网络管理员联系！", "严重错误：", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            ModelToUI(materielNow);
        }

        public void setMaterielAndPlace(string mname, string mid, int thick, int single, string standard, int length, int width, string pname, string pid, int plength, int pwidth, int count)
        {
            throw new NotImplementedException();
        }

        #endregion

        /// <summary>
        /// 显示model
        /// </summary>
        /// <param name="materiel"></param>
        void ModelToUI(T_JB_Materiel materiel)
        {
            this.txtId.Text = materiel.C_id;
            this.txtMaterielName.Text = materiel.C_name;
            this.cmbType.SelectedValue = materiel.C_type;
            this.txtStand.Text = materiel.C_standerd;
            this.txtMT.Text = materiel.I_thick.ToString();
            this.txtLT.Text = materiel.I_length.ToString();
            this.txtWT.Text = materiel.I_width.ToString();
            this.cmbArea.SelectedValue = materiel.C_area;

            this.txtMeno.Text = materiel.C_memo;
            this.txtDim1.Text = materiel.Dec_dimension1.ToString();
            this.txtDim2.Text = materiel.Dec_dimension2.ToString();
            this.txtDim3.Text = materiel.Dec_dimension3.ToString();
            this.txtAngle.Text = materiel.Dec_angle.ToString();
            this.txtRL.Text = materiel.C_regrinding_length;

            cbBoard.Checked = materiel.I_buy == 1;
        }

        private void txtId_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (string.IsNullOrEmpty(cmbMachine.Text.Trim()))
                {
                    MessageBox.Show("请先选择机床！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                string mes = string.Empty;
                List<T_JB_Materiel> listKnife = new List<T_JB_Materiel>();

                //验证码
                if (!CheckBarcode(txtId.Text.Trim(), ref mes, ref listKnife, ref blank))
                {
                    MessageBox.Show(mes, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                dtBak = dt.Clone();

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < listKnife.Count; i++)
                {
                    int count1 = 0, count2 = 0, count3 = 0, count4 = 0;
                    int c1 = -1, c2 = -1, c3 = -1, c4 = -1;
                    bool b1 = false, b2 = false, b3 = false, b4 = false;

                    getMachineKnife(cmbMachine.Text.Trim(), listKnife[i].C_id, out count1, out count2, out count3, out count4);//已有
                    DataTable st = stockBll.getStocksKnifeList(null, null, null, null, null, listKnife[i].C_id);//库存

                    if (count1 == 0)
                    {
                        for (int j = 0; j < st.Rows.Count; j++)
                        {
                            if (Convert.ToInt32(st.Rows[j][5]) > 0)
                            {
                                c1 = j;
                                break;
                            }
                        }

                        if (c1 != -1)
                        {
                            DataRow dr = dtBak.NewRow();
                            dr[0] = listKnife[i].C_id;
                            dr[1] = listKnife[i].C_name;
                            dr[2] = listKnife[i].C_standerd;
                            dr[4] = st.Rows[c1][4].ToString();//货位
                            dr[6] = Global.longid;

                            dr[7] = 1;
                            dr[8] = 0;
                            dr[9] = 0;
                            dr[10] = 0;
                            dr[11] = st.Rows[c1][9].ToString();

                            dtBak.Rows.Add(dr);
                            //sb.Append("  上模可出\r\n");
                        }
                        else
                        {
                            b1 = true;
                        }
                    }

                    if (count2 == 0)
                    {
                        if (blank <= 2)
                        {
                            for (int j = 0; j < st.Rows.Count; j++)
                            {
                                if (Convert.ToInt32(st.Rows[j][6]) > 0)
                                {
                                    c2 = j;
                                    break;
                                }
                            }

                            if (c2 != -1)
                            {
                                DataRow dr = dtBak.NewRow();
                                dr[0] = listKnife[i].C_id;
                                dr[1] = listKnife[i].C_name;
                                dr[2] = listKnife[i].C_standerd;
                                dr[4] = st.Rows[c2][4].ToString();//货位
                                dr[6] = Global.longid;

                                dr[7] = 0;
                                dr[8] = 1;
                                dr[9] = 0;
                                dr[10] = 0;
                                dr[11] = st.Rows[c2][9].ToString();

                                dtBak.Rows.Add(dr);
                                //sb.Append("  0.25可出\r\n");
                            }
                            else
                            {
                                //sb.Append("  0.25无货\r\n");
                                b2 = true;
                            }
                        }
                    }

                    if (count3 == 0)
                    {
                        if (blank > 2)
                        {
                            for (int j = 0; j < st.Rows.Count; j++)
                            {
                                if (Convert.ToInt32(st.Rows[j][7]) > 0)
                                {
                                    c3 = j;
                                    break;
                                }
                            }

                            if (c3 != -1)
                            {
                                DataRow dr = dtBak.NewRow();
                                dr[0] = listKnife[i].C_id;
                                dr[1] = listKnife[i].C_name;
                                dr[2] = listKnife[i].C_standerd;
                                dr[4] = st.Rows[c3][4].ToString();//货位
                                dr[6] = Global.longid;

                                dr[7] = 0;
                                dr[8] = 0;
                                dr[9] = 1;
                                dr[10] = 0;
                                dr[11] = st.Rows[c3][9].ToString();

                                dtBak.Rows.Add(dr);
                                //sb.Append("  0.5可出\r\n");
                            }
                            else
                            {
                                //sb.Append("  0.5无货\r\n");
                                b3 = true;
                            }
                        }
                    }

                    if (count4 == 0)
                    {
                        if (listKnife[i].I_buy == 1)
                        {
                            for (int j = 0; j < st.Rows.Count; j++)
                            {
                                if (Convert.ToInt32(st.Rows[j][8]) > 0)
                                {
                                    c4 = j;
                                    break;
                                }
                            }

                            if (c4 != -1)
                            {
                                DataRow dr = dtBak.NewRow();
                                dr[0] = listKnife[i].C_id;
                                dr[1] = listKnife[i].C_name;
                                dr[2] = listKnife[i].C_standerd;
                                dr[4] = st.Rows[c4][4].ToString();//货位
                                dr[6] = Global.longid;

                                dr[7] = 0;
                                dr[8] = 0;
                                dr[9] = 0;
                                dr[10] = 1;
                                dr[11] = st.Rows[c4][9].ToString();

                                dtBak.Rows.Add(dr);
                                //sb.Append("  退料板可出\r\n");
                            }
                            else
                            {
                                //sb.Append("  退料板无货\r\n");
                                b4 = true;
                            }
                        }
                    }

                    if (b1 || b2 || b3 || b4)
                    {//存在无货
                        sb.Append(string.Format("\r\n刀具编码 {0}\r\n", listKnife[i].C_id));
                        if (b1)
                        {
                            sb.Append("  上模无货\r\n");
                        }
                        if (b2)
                        {
                            sb.Append("  0.25无货\r\n");
                        }
                        if (b3)
                        {
                            sb.Append("  0.5无货\r\n");
                        }
                        if (b4)
                        {
                            sb.Append("  退料板无货\r\n");
                        }

                        DataTable dtkd = bll.getKDList(Global.minValue, Global.minValue, null, null, InOutType.KNIFE_OUT_USE, listKnife[i].C_id, null);
                        if (dtkd.Rows.Count > 0)
                        {
                            List<string> list = new List<string>();
                            for (int j = 0; j < dtkd.Rows.Count; j++)
                            {
                                if (!list.Contains(dtkd.Rows[j][5].ToString()))
                                {
                                    list.Add(dtkd.Rows[j][5].ToString());
                                }
                            }

                            sb.Append(string.Format("  该刀具在 {0}\r\n", string.Join(",", list.ToArray())));
                        }
                        else
                        {
                            sb.Append("  机床上无该刀具！\r\n");
                        }
                    }
                    else if (c1+c2+c3+c4==-4)
                    {
                        sb.Append(string.Format("\r\n刀具编码 {0}\r\n", listKnife[i].C_id));
                        sb.Append(string.Format("  该刀具已经在 {0}！\r\n", cmbMachine.Text.Trim()));
                        continue;
                    }
                }

                sb.Append("\r\n是否添加到列表？");

                DialogResult res = MessageBox.Show(sb.ToString(), "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

                if (res.Equals(DialogResult.Yes))
                {
                    addRowMult(dtBak);
                }

                initSub();
            }
        }

        private void getMachineKnife(string machine, string mid, out int count1, out int count2, out int count3, out int count4)
        {
            DataTable st = bll.getKDList(Global.minValue, Global.minValue, null, cmbMachine.Text.Trim(), InOutType.KNIFE_OUT_USE, mid, null);
            count1 = 0; count2 = 0; count3 = 0; count4 = 0;

            for (int i = 0; i < st.Rows.Count; i++)
            {
                count1 += (int)Convert.ToSingle(st.Rows[i][8].ToString());
                count2 += (int)Convert.ToSingle(st.Rows[i][9].ToString());
                count3 += (int)Convert.ToSingle(st.Rows[i][10].ToString());
                count4 += (int)Convert.ToSingle(st.Rows[i][11].ToString());
            }

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (cmbMachine.Text.Trim().Equals(dt.Rows[i][10].ToString()) && mid.Equals(dt.Rows[i][0].ToString()))
                {
                    count1 += (int)Convert.ToSingle(dt.Rows[i][3].ToString());
                    count2 += (int)Convert.ToSingle(dt.Rows[i][4].ToString());
                    count3 += (int)Convert.ToSingle(dt.Rows[i][5].ToString());
                    count4 += (int)Convert.ToSingle(dt.Rows[i][6].ToString());
                }
            }

            count1 = count1 > 0 ? 1 : 0;
            count2 = count2 > 0 ? 1 : 0;
            count3 = count3 > 0 ? 1 : 0;
            count4 = count4 > 0 ? 1 : 0;
        }
    }
}
